
<#if document.hasAspect('caas:locatable')>
<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA_97Ip04_pcg4-y91fqk8iRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSGnboiyY1VzOyB-taGLpsEHgQ7sQ" type="text/javascript"></script>
<script type="text/javascript" src="${TemplateUtils.context}/scripts/jquery-1.2.4a.js"></script>
<script type="text/javascript" src="${TemplateUtils.context}/scripts/mapstraction.js"></script>
<script type="text/javascript" src="${TemplateUtils.context}/scripts/mapstraction-geocode.js"></script>


<div style="width: 600px; height: 400px;" id="map">

</div>

<div>
<p>
<input class="map-control" type="button" id="defaultLocation" value="Set default location..."></input>
<input class="map-control" type="button" id="clickToggle" value="Select location..."></input>
<input class="map-control" type="button" id="saveLocation" value="Save location..."></input>
</p>
<!--
<p><input class="map-control" type="button" id="nearbyItems" value="Nearby docs..."></input></p>
--->
</div>


<script type="text/javascript">
$().ready(function() {
	
	mapstraction = new Mapstraction('map','google');
	mapstraction.doSelection = false;
	
	mapstraction.addControls({
	    zoom: 'large',
		overview: true,
		scale: true
	});
	
	mapstraction.setDocMarker = function(lat, lon, o) {
		if (o == null) o = {};
		var myPoint = new LatLonPoint(lat, lon);
		if (!o.nocenter) {
			this.setCenterAndZoom(myPoint, 10);
		}
		if (this.docMarker != null) {
			this.removeMarker(this.docMarker);
		}
		this.docMarker = new Marker(myPoint);
		if (o.bubble == null) {
			o.bubble = lat+" : "+lon;
		}
		if (o.bubble != null) {
			this.docMarker.setInfoBubble(o.bubble);
		}
		this.addMarker(this.docMarker);
	};
	
	mapstraction.setDefaultLocation = function (address)
	{
		if (address == null) {
		// get the current user's object and get the address off it. 
			address = "${person.properties['cm:organizationId']}";
		} 
		
		if (address == null || address.length == 0) {
			alert("No default address found");
			return;
		}
		// Look that address up
		var geocoder = new MapstractionGeocoder(function(result) {
			mapstraction.setDocMarker(result.point.lat, result.point.lon);
			
		}, 'google');
		// address = "queensbridge street, southbank, 3006";
		
		geocoder.geocode({'address': address});

	}
	
	<#if document.properties['caas:latitude']?has_content && document.properties['caas:longitude']?has_content>
		mapstraction.setDocMarker(${document.properties['caas:latitude']},${document.properties['caas:longitude']}, {center: true});
		
	<#else>
		mapstraction.setDocMarker(-37.814251,144.963169);
		<#if document.properties['caas:location']?has_content>
			// Do a geocode lookup on the location
			mapstraction.setDefaultLocation('${document.properties['caas:location']}');
		</#if>
	</#if>

	mapstraction.clickHandler = function(lat, lon, point) {
		if (this.doSelection) {
			this.doSelection = false;
			mapstraction.setDocMarker(lat, lon, {nocenter: true});
		}
		enableButtons();
	};

	$('#clickToggle').click(function() {
		mapstraction.doSelection = true;
		disableButtons();
		
	});
	
	$('#saveLocation').click(function() {
		if (mapstraction.docMarker == null) {
			return;
		}
		disableButtons();
		var lat = mapstraction.docMarker.location.lat;
		var lon = mapstraction.docMarker.location.lon;
		
		// Find out the place name
		$.getJSON('http://ws.geonames.org/findNearbyPlaceNameJSON?jsoncallback=?&callback=updateLocation&lat='+lat+'&lng='+lon, function(data) {
		});
	});

	$('#defaultLocation').click(function() {
		mapstraction.setDefaultLocation();
	});
	
	$('#nearbyItems').click(function() {
		var marker = mapstraction.docMarker;
		var curLat = marker.location.lat;
		var curLon = marker.location.lon;
		
		if (mapstraction.nearbyDocs != null && mapstraction.nearbyDocs.length > 0) {
			mapstraction.nearbyDocs.each (function(item) {
				mapstraction.removeMarker(item);
			});
		}
		
		mapstraction.nearbyDocs = new Array();
		
		$.getJSON('${TemplateUtils.context}/service/caas/nearbydocs?lat='+curLat+'&lon='+curLon, function(data) {
			data.each (function(item) {
				if (mapstraction.docMarker.location.lat == item.lat && mapstraction.docMarker.location.lon == item.lon) {
					
				} else {
					var myPoint = new LatLonPoint(item.lat, item.lon);
					var theMarker = new Marker(myPoint);
					theMarker.setInfoBubble(item.name);
					theMarker.setIcon('${TemplateUtils.context}/scripts/dd-start.png');
					mapstraction.addMarker(theMarker);
					mapstraction.nearbyDocs[mapstraction.nearbyDocs.length] = theMarker;
				}
			});
		});
	});
});

function updateLocation(data)
{
	var lat = mapstraction.docMarker.location.lat;
	var lon = mapstraction.docMarker.location.lon;
	var name = "";
	if (data.geonames && data.geonames.length > 0) {
		name = data.geonames[0].name;
	}
	$.post('${TemplateUtils.context}/service/caas/updatelocation', {id: '${document.id}', "lat": lat, "lon": lon, "name": name}, function (data) {
				alert("Location saved to "+lat+","+lon+", near "+name);
				enableButtons();
			});
}

function enableButtons()
{
	$('.map-control').each(function() {
		this.disabled = false;
	});
}

function disableButtons()
{
	$('.map-control').each(function() {
		this.disabled = true;
	});
}


</script>
</#if>